<?php
/**
 * Created by PhpStorm.
 * User: River
 * Email: river0428@gmail.com
 * Date: 2018/10/9
 * Time: 下午2:20
 */

namespace Modules\Api\Controllers;

use EasyWeChat\Factory;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class WXMessage extends Controller
{
    public function index()
    {
        Log::info('微信消息回调');

        $app = Factory::officialAccount(config('wechat'));

        /*$app->server->push(function ($message) use ($app) {
            Log::info('message:' . json_encode($message));
            switch ($message['MsgType']) {
                case 'event':

                    // 扫描临时二维码绑定操作员
                    if ($message['EventKey']) {
                        $scene = str_replace('qrscene_', '', $message['EventKey']);
                        $data  = Cache::get($scene);
                        Log::info('场景id获取的缓存数据' . json_encode($data));
                        if ($data) {
                            $data['is_scan'] = 1;
                            $data['open_id'] = $message['FromUserName'];

                            $user = $app->user->get($data['open_id']);
                            $data['nickname'] = $user['nickname'];
                            $data['nickname'] = $user['nickname'];
                            $data['head_img'] = $user['headimgurl'];
                            Cache::put($scene, $data, 5);
                            if (!TicketOperator::where([
                                'ticket_id' => $data['ticket_id'],
                                'open_id'   => $data['open_id']
                            ])->first()
                            ) {
                                TicketOperator::create([
                                    'open_id'   => $data['open_id'],
                                    'nickname'  => $data['nickname'],
                                    'head_img'  => $data['head_img'],
                                    'ticket_id' => $data['ticket_id'],
                                ]);
                            }
                        }
                    }

                    break;
                default:
                    break;
            }

            // ...
        });*/

        $response = $app->server->serve();

        return $response;
    }
}